package com.nanxun.strategy.Impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanxun.config.WxParamConfig;
import  com.nanxun.constant.AuthConstants;
import com.nanxun.domain.LoginMember;
import com.nanxun.domain.Member;
import com.nanxun.mapper.LoginMemberMapper;
import com.nanxun.model.SecurityUser;
import com.nanxun.strategy.LoginStrategy;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;

/**
 * 商城用户购物系统登录策略的具体实现
 * */

@Service(AuthConstants.MEMBER_LOGIN)//具体策略放到容器里面
public class MemberLoginStrategy implements LoginStrategy {

    @Resource
    private WxParamConfig wxParamConfig;
    @Resource
    private LoginMemberMapper loginMemberMapper;

    @Override
    public UserDetails realLogin(String username) {
        //调用微信接口服务器中的：登录凭证校验接口（appid，appsecret，code）
  //      String url=" https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
        //获取登录凭证校验接口url
        String realUrl = String.format(wxParamConfig.getUrl(), wxParamConfig.getAppid(), wxParamConfig.getSecret(), username);
        //使用get方法调用登录凭证校验接口
        String JsonStr = HttpUtil.get(realUrl);
        //判断响应是否有值
        if(!StringUtils.hasText(JsonStr)){
            throw new InternalAuthenticationServiceException("登录异常，请重试");
        }
        //使用fastjson将登录凭证校验接口响应的json格式的字符串转换为json对象
        JSONObject jsonObject = JSONObject.parseObject(JsonStr);
        //获取openid
        String openid = jsonObject.getString("openid");
        //判断是否有值
        if(!StringUtils.hasText(openid)){
            throw new InternalAuthenticationServiceException("登录异常，请重试");
        }
        //根据会员openid查询会员对象
        LoginMember loginMember = loginMemberMapper.selectOne(new LambdaQueryWrapper<LoginMember>()
                .eq(LoginMember::getOpenId, openid)
        );
        //判断会员是否存在
        if (ObjectUtil.isNull(loginMember)){
            //会员不存在
            //注册：创建会员对象到我们的微信小程序的用户体系内
            loginMember=registerMember(openid);
        }
        //判断会员账号的状态
        if(!loginMember.getStatus().equals(1)){
            throw new InternalAuthenticationServiceException("账号异常，请联系平台工作人员");
        }
        //说明会员账号的账号正常
        //会员存在：返回security框架能够认识的安全用户对象SecurityUser
        SecurityUser securityUser = new SecurityUser();
        securityUser.setUserId(Long.valueOf(loginMember.getId()));
        securityUser.setLoginType(AuthConstants.MEMBER_LOGIN);
        securityUser.setUsername(openid);
        securityUser.setStatus(loginMember.getStatus());
        securityUser.setPassword(wxParamConfig.getPwd());
        securityUser.setOpenid(openid);
        return securityUser;
    }

    private LoginMember registerMember(String openid) {
        ServletRequestAttributes requestAttributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String ip = request.getRemoteAddr();
        LoginMember loginMember = new LoginMember();
        loginMember.setOpenId(openid);
        loginMember.setStatus(1);
        loginMember.setCreateTime(new Date());
        loginMember.setUpdateTime(new Date());
        loginMember.setUserLasttime(new Date());
        loginMember.setUserRegip(ip);
        loginMember.setUserLastip(ip);
        //如果有积分业务
        loginMember.setScore(0);

        //新增会员
        loginMemberMapper.insert(loginMember);
        return loginMember;
    }

  /*  public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("WECHAT"));
    }*/
}
